// 导入用户集合
const { Goods } = require('../../../model/goods');
// 引入分页模块
const pagination = require('mongoose-sex-page')
const { goodsState } = require('../../../enum/state')

module.exports = async (req, res) => {
  const { index, size, category, keyword } = req.query
  let { sortName, sort } = req.query
  console.log('req.query', req.query)
  const query = {
    state: goodsState.published
  }
  if (category) {
    query.category = category
  }
  if(keyword) {
    query.title = {$regex: keyword}
  }
  if (!sortName) {
    sortName = '_id'
  }
  if (!sort) {
    sort = -1
  }
  console.log(sortName, sort)
  console.log('query', query)
  const list = await pagination(Goods).find(query).sort({[sortName]: sort}).page(index).size(size).populate('category').populate('user').exec()
  res.json({
      data: list
  })
}